--[[
     Created by IntelliJ IDEA.
     User: 毛凯
     Date: 2018/7/30
     Time: 9:38
     参数：ARGV[1]:活动id
     参数：ARGV[2]:标记  1，随机红包，2点赞红包，3被点赞红包
     返回：返回红包金额（单位分）
--]]

--记录活动红包以及人数key
local ACTINFOKEY = 'act_'..ARGV[1]
-- 记录初始化信息key
local ACTINITKEY = 'actInit_'..ARGV[1]

--标记
local flag = tonumber(ARGV[2]) or 0
--最小值
local min = 0
--最大值
local max = 0
--剩余总奖金
local rewardRemaining = 0
--已发奖金人数
local rewardUserCnt = 0
--红包概率字段
local center = 0
--活动id
local act = tonumber(ARGV[1]) or 0

--flag为1是获取随机红包值
if(flag == 1)
then
    min = tonumber(redis.call('hget',ACTINITKEY,'randomMin')) or 0
    max = tonumber(redis.call('hget',ACTINITKEY,'randomMax')) or 0
    rewardRemaining = tonumber(redis.call('hget',ACTINFOKEY,'randomRemaining')) or 0
    rewardUserCnt = tonumber(redis.call('hget',ACTINFOKEY,'randomParticipants')) or 0
    center = tonumber(redis.call('hget',ACTINITKEY,'randomCenter')) or 0
end
--flag为2是获取随点赞红包
if(flag == 2)
then
    min = tonumber(redis.call('hget',ACTINITKEY,'likeMin')) or 0
    max = tonumber(redis.call('hget',ACTINITKEY,'likeMax')) or 0
    rewardRemaining = tonumber(redis.call('hget',ACTINFOKEY,'likeRemaining')) or 0
    rewardUserCnt = tonumber(redis.call('hget',ACTINFOKEY,'likeParticipants')) or 0
    center = tonumber(redis.call('hget',ACTINITKEY,'likeCenter')) or 0
end
--flag为3是获取被点赞红包
if(flag == 3)
then
    min = tonumber(redis.call('hget',ACTINITKEY,'praisedMin')) or 0
    max = tonumber(redis.call('hget',ACTINITKEY,'praisedMax')) or 0
    rewardRemaining = tonumber(redis.call('hget',ACTINFOKEY,'praisedRemaining')) or 0
    rewardUserCnt = tonumber(redis.call('hget',ACTINFOKEY,'praisedParticipants')) or 0
    center = tonumber(redis.call('hget',ACTINITKEY,'praisedCenter')) or 0
end

local redPackage= math.randomMoney(act,rewardRemaining,min,max,center)

return redPackage